//
//  UIImage+SWImage.m
//  greenbuilding
//
//  Created by lvjian01 on 2017/9/13.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "UIImage+SWImage.h"

@implementation UIImage (SWImage)

+ (UIImage *)createNaviWithColor:(UIColor *)color{
    CGRect rect=CGRectMake(0.0f, 0.0f, Main_Screen_Width, 64.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;

}

+ (UIImage *)createImageWithColor:(UIColor *)color{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

-(NSData *)compressImageToMaxDataSizeKBytes:(CGFloat)size{
    NSData * data = UIImageJPEGRepresentation(self, 1.0);
    CGFloat dataKBytes = data.length/1000.0;
    CGFloat maxQuality = 0.9f;
    CGFloat lastData = dataKBytes;
    while (dataKBytes > size && maxQuality > 0.01f) {
        maxQuality = maxQuality - 0.01f;
        data = UIImageJPEGRepresentation(self, maxQuality);
        dataKBytes = data.length / 1000.0;
        if (lastData == dataKBytes) {
            break;
        }else{
            lastData = dataKBytes;
        }
        
    }
    return data;
}

+(instancetype)sw_placeHolderImage{
    return [UIImage imageNamed:placeHolderImage];
}

+ (instancetype)sw_debugImage{
    return [UIImage imageNamed:@"sw_debugImage.jpg"];
}

@end
